home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / alv.sun / alv.lha / src / transform.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  5.5 KB  |  219 lines

  1. #include <stdio.h>
  2. #include "defs.h"
  3. #include "matrix.h"
  4.  
  5. har   *progname;
  6. har   *filename;
  7. ixrect *pr1, *pr2;
  8.  
  9. #ifdef STANDALONE
  10. ain(argc, argv, envp)
  11. #else
  12. ransform_main(argc, argv, envp)
  13. #endif
  14.     int     argc;
  15.     char  **argv;
  16.     char  **envp;
  17. {
  18.     register int i, j;
  19.     float   a, xs, ys;
  20.     int     x, y;
  21.     int     rotation, shearing;
  22.     int     levels;
  23.     int     max_xsize, min_xsize, max_ysize, min_ysize, tr_xsize, tr_ysize;
  24.     VECTOR  start, stop;
  25.     VECTOR  tr_start, tr_stop;
  26.     VECTOR  top_left, top_right, bottom_left, bottom_right;
  27.     VECTOR  tr_top_left, tr_top_right, tr_bottom_left, tr_bottom_right;
  28.     MATRIX  transform;
  29.     colormap_t    colormap;
  30.  
  31.     warning("THIS CODE STILL UNDER DEVELOPMENT\nDO NOT TRUST");
  32.     a = M_PI / 2.;
  33.     xs = ys = 0.;
  34.     x = y = 0;
  35.     rotation = shearing = FALSE;
  36.     progname = strsave(argv[0]);
  37.     parse_profile(&argc, argv, envp);
  38.  
  39.     while ((gc = getopt(argc, argv, "X:Y:a:x:y:")) != EOF)
  40.         switch (gc) {
  41.         case 'x':
  42.             if (!shearing) {
  43.                 rotation = TRUE;
  44.                 x = atoi(optarg);
  45.             } else
  46.                 error("Cannot rotate and shear in same invocation");
  47.             break;
  48.         case 'y':
  49.             if (!shearing) {
  50.                 rotation = TRUE;
  51.                 y = atoi(optarg);
  52.             } else
  53.                 error("Cannot rotate and shear in same invocation");
  54.             break;
  55.         case 'a':
  56.             if (!shearing) {
  57.                 rotation = TRUE;
  58.                 a = atof(optarg);
  59.             } else
  60.                 error("Cannot rotate and shear in same invocation");
  61.             break;
  62.         case 'X':
  63.             if (!rotation) {
  64.                 shearing = TRUE;
  65.                 xs = atof(optarg);
  66.             } else
  67.                 error("Cannot rotate and shear in same invocation");
  68.             break;
  69.         case 'Y':
  70.             if (!rotation) {
  71.                 shearing = TRUE;
  72.                 ys = atof(optarg);
  73.             } else
  74.                 error("Cannot rotate and shear in same invocation");
  75.             break;
  76.         case '?':
  77.             errflag++;
  78.             break;
  79.         }
  80.  
  81.     if (errflag)
  82.         error((char *) 0, "Usage: %s: [[-x xcoord] [-y ycoord] [-a angle]] | [[-X xshear] [-Y yshear]] [infile] [outfile]\n", progname);
  83.  
  84.     for (stream = 0; optind < argc; stream++, optind++)
  85.         if (stream < 2 && strcmp(argv[optind], "-") != 0)
  86.             if (freopen(argv[optind], mode[stream], f[stream]) == NULL)
  87.                 error("%s %s", PR_IO_ERR_INFILE, argv[optind]);
  88.  
  89.     if ((pr1 = pr_load(stdin, &colormap)) == NULL)
  90.         error(PR_IO_ERR_RASREAD);
  91.  
  92.     top_left = vinit(0., 0., 0., 1.);
  93.     top_right = vinit((double) pr1->pr_size.x, 0., 0., 1.);
  94.     bottom_right = vinit((double) pr1->pr_size.x, (double) pr1->pr_size.y, 0., 1.);
  95.     bottom_left = vinit(0., (double) pr1->pr_size.y, 0., 1.);
  96.  
  97.     warning("top_left = ");
  98.     dbgvprint(top_left);
  99.     warning("top_right = ");
  100.     dbgvprint(top_right);
  101.     warning("bottom_right = ");
  102.     dbgvprint(bottom_right);
  103.     warning("bottom_left = ");
  104.     dbgvprint(bottom_left);
  105.  
  106.     if (rotation)
  107.         transform = minit(cos(a), sin(a), 0., 0., -sin(a), cos(a), 0., 0., (1 - cos(a)) * x + y * sin(a), (1 - cos(a)) * y - x * sin(a), 1., 0., 0., 0., 0., 1.);
  108.     else
  109.         transform = minit(1., ys, 0., 0., xs, 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.);
  110.  
  111.     dbgmprint(transform);
  112.     tr_top_left = vnil();
  113.     tr_top_right = vnil();
  114.     tr_bottom_left = vnil();
  115.     tr_bottom_right = vnil();
  116.  
  117.     vmult(top_left, transform, tr_top_left);
  118.     vmult(top_right, transform, tr_top_right);
  119.     vmult(bottom_left, transform, tr_bottom_left);
  120.     vmult(bottom_right, transform, tr_bottom_right);
  121.  
  122.     warning("tr_top_left = ");
  123.     dbgvprint(tr_top_left);
  124.  
  125.     warning("tr_top_right = ");
  126.     dbgvprint(tr_top_right);
  127.  
  128.     warning("tr_bottom_right = ");
  129.     dbgvprint(tr_bottom_right);
  130.  
  131.     warning("tr_bottom_left = ");
  132.     dbgvprint(tr_bottom_left);
  133.  
  134.     max_xsize = (int) MAX(tr_top_left[0], tr_top_right[0]);
  135.     max_xsize = (int) MAX(max_xsize, tr_bottom_left[0]);
  136.     max_xsize = (int) MAX(max_xsize, tr_bottom_right[0]);
  137.  
  138.     min_xsize = (int) MIN(tr_top_left[0], tr_top_right[0]);
  139.     min_xsize = (int) MIN(min_xsize, tr_bottom_left[0]);
  140.     min_xsize = (int) MIN(min_xsize, tr_bottom_right[0]);
  141.  
  142.     max_ysize = (int) MAX(tr_top_left[1], tr_top_right[1]);
  143.     max_ysize = (int) MAX(max_ysize, tr_bottom_left[1]);
  144.     max_ysize = (int) MAX(max_ysize, tr_bottom_right[1]);
  145.  
  146.     min_ysize = (int) MIN(tr_top_left[1], tr_top_right[1]);
  147.     min_ysize = (int) MIN(min_ysize, tr_bottom_left[1]);
  148.     min_ysize = (int) MIN(min_ysize, tr_bottom_right[1]);
  149.  
  150.     tr_xsize = abs(max_xsize - min_xsize);
  151.     tr_ysize = abs(max_ysize - min_xsize);
  152.  
  153.     warning("xsize = %d, ysize = %d", tr_xsize, tr_ysize);
  154.     warning("minxsize = %d, min_ysize = %d", min_xsize, min_ysize);
  155.  
  156.     if ((pr2 = mem_create(tr_xsize, tr_ysize, pr1->pr_depth)) == NULL)
  157.         error("mem_create returned NULL");
  158.  
  159.     tr_start = vnil();
  160.     tr_stop = vnil();
  161.     for (j = 0; j < pr1->pr_size.y; j++) {
  162.         start = vinit(0., (double) j, 0., 1.);
  163.         stop = vinit((double) (pr1->pr_size.x), (double) j, 0., 1.);
  164.         vmult(start, transform, tr_start);
  165.         tr_start[0] += abs(min_xsize);
  166.         tr_start[1] += abs(min_ysize);
  167.         vmult(stop, transform, tr_stop);
  168.         tr_stop[0] += abs(min_xsize);
  169.         tr_stop[1] += abs(min_ysize);
  170.         bres_line((int) tr_start[0], (int) tr_start[1], (int) tr_stop[0], (int) tr_stop[1], j);
  171.     }
  172.  
  173.     pr_dump(pr2, stdout, &colormap, RT_STANDARD, 0);
  174. }
  175. #define SIGN(a)    ((a<0)?-1:((a>0)?1:0))
  176.  
  177. /* line end pts are (x1, y1) & (x2, y2). Assumed not equal */
  178. res_line(x1, y1, x2, y2, iny)
  179.     int     x1, y1, x2, y2, iny;
  180. {
  181.     register int i;
  182.     int     temp;
  183.     int     x, y;
  184.     int     dx, dy, s1, s2, e, interchange;
  185.  
  186.     x = x1;
  187.     y = y1;
  188.     dx = abs(x2 - x1);
  189.     dy = abs(y2 - y1);
  190.     s1 = SIGN(x2 - x1);
  191.     s2 = SIGN(y2 - y1);
  192.  
  193.     if (dy > dx) {
  194.         temp = dx;
  195.         dx = dy;
  196.         dy = temp;
  197.         interchange = FALSE;
  198.     } else
  199.         interchange = TRUE;
  200.  
  201.     e = 2 * dy - dx;
  202.  
  203.     for (i = 0; i < dx; i++) {
  204.         pr_put(pr2, x, y, pr_get(pr1, i, iny));
  205.         while (e >= 0) {
  206.             if (interchange)
  207.                 x += s1;
  208.             else
  209.                 y += s2;
  210.             e -= 2 * dx;
  211.         }
  212.         if (interchange)
  213.             y += s2;
  214.         else
  215.             x += s1;
  216.         e += 2 * dy;
  217.     }
  218. }
  219.